﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;
using System.Windows.Forms.VisualStyles;
using System.Windows.Shapes;
using Macad.Core.Shapes;
using Macad.Occt;
using Microsoft.CodeAnalysis;
using static SpCad.Tools.ShapeTools;

namespace SpCad.Tools
{
    public enum GeomType
    {
        /// <summary>
        /// 线
        /// </summary>
        Line,
        /// <summary>
        /// 圆
        /// </summary>
        Circle,
        /// <summary>
        /// 椭圆
        /// </summary>
        Ellipse,
        /// <summary>
        /// 弧线
        /// </summary>
        Arc,
        /// <summary>
        /// 贝塞尔曲线
        /// </summary>
        BezierCurve

    }
    /// <summary>
    /// 线
    /// </summary>
    public class ShapeLine : ShapeBase
    {
        public string Title = "线";
        public Geom_Line? Geom { get; private set; }
        /// <summary>
        /// 位置信息
        /// </summary>
        public Ax1? Position => Geom?.Position();
        /// <summary>
        /// 位置
        /// </summary>
        public Pnt  Location => Geom.Position().Location;
        public ShapeLine(TopoDS_Shape tshape, Geom_Line curve, Pnt startPoint, Pnt endPoint) : base(tshape, curve, startPoint, endPoint)
        {
            Geom = curve; 
            base.Type = GeomType.Line;

        }

        public double GetLength()
        {
            var props = new GProp_GProps();
            BRepGProp.LinearProperties(TShape, props);
            return props.Mass();
        }
        public override Pnt GetLocation()
        {
            return Location;
        }

        public override bool IsEqual(ShapeBase shape)
        {
            try
            {
                if (this.Type != shape.Type) return false;
                var bshap = shape as ShapeLine;
                if (this.Location != bshap?.Location) return false;

                if (!Geom.Position().Direction.Equals(Geom?.Position().Direction)) return false;

              
                if (this.StartPoint == bshap.StartPoint || this.StartPoint == base.EndPoint)
                {

                    if (this.EndPoint == bshap.EndPoint || this.EndPoint == base.StartPoint)
                    {
                        return true;
                    }
                }
               

                return false;
            }
            catch (Exception)
            {

                return false;
            }
         
        }
    }
    /// <summary>
    /// 圆
    /// </summary>
    public class ShapeCircle : ShapeBase, IshapeType
    {
     

        public string Title = "圆";
        public Geom_Circle? Geom { get; private set; }
       
        public ShapeCircle(TopoDS_Shape tshape, Geom_Circle curve, Pnt startPoint, Pnt endPoint) : base(tshape, curve, startPoint, endPoint)
        {
           Type = GeomType.Circle;
            Geom = curve;
        }

        /// <summary>
        /// 位置信息
        /// </summary>
        public Ax2 Position => Geom.Position();
        /// <summary>
        /// 位置
        /// </summary>
        public Pnt Location => this.Geom.Position().Location;
        /// <summary>
        /// 半径
        /// </summary>
        public double Radius => Geom.Radius();

        public object XDirection => Position.XDirection;
        public object YDirection => Position.YDirection;

        public override Pnt GetLocation()
        {
            return Location;
        }

        public override bool IsEqual(ShapeBase shape)
        {
            try
            {
                if (this.Type != shape.Type) return false;
                if (this.StartPoint != shape.StartPoint || this.EndPoint != shape.EndPoint) return false;
                if (Geom.Position().Direction != ((ShapeCircle)shape)?.Geom?.Position().Direction) return false;
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }
    }
    /// <summary>
    /// 椭圆
    /// </summary>
    public class ShapeEllipse : ShapeBase, IshapeType
    {
   
        public Ax2 Position => Geom.Position();
        public string Title = "椭圆";
        /// <summary>
        /// 位置
        /// </summary>
        public Pnt Location => this.Geom.Position().Location;
        public Geom_Ellipse? Geom{ get; private set; }

        public ShapeEllipse(TopoDS_Shape tshape, Geom_Ellipse curve, Pnt startPoint, Pnt endPoint) : base(tshape, curve, startPoint, endPoint)
        {
            Geom = curve;
            Type = GeomType.Ellipse;
        }

        public override Pnt GetLocation()
        {
            return Location;
        }

        public override bool IsEqual(ShapeBase shape)
        {
            try
            {
                if (this.Type != shape.Type) return false;
                if (this.StartPoint != shape.StartPoint || this.EndPoint != shape.EndPoint) return false;
                if (Geom.Position().Direction != ((ShapeEllipse)shape)?.Geom?.Position().Direction) return false;
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }
    }
    /// <summary>
    /// 圆弧
    /// </summary>
    public class ShapeArc : ShapeBase, IshapeType
    {
        public string Title = "圆弧";
         
        /// <summary>
        /// 位置
        /// </summary>
        public Pnt Location =>  Geom.StartPoint();

        public Geom_BezierCurve? Geom { get; private set; }

        public ShapeArc(TopoDS_Shape tshape, Geom_BezierCurve curve, Pnt startPoint, Pnt endPoint) : base(tshape, curve, startPoint, endPoint)
        {
            Geom = curve;
            Geom_Curve gom = curve;
            Type = GeomType.Arc;
        }

        public override Pnt GetLocation()
        {
            return Location;
        }

        public override bool IsEqual(ShapeBase shape)
        {
            try
            {
                if (this.Type != shape.Type) return false;
                if (this.StartPoint != shape.StartPoint || this.EndPoint != shape.EndPoint) return false;
                if (Geom.Equals(shape.Geom)) return false;
           
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }
    }
   
    /// <summary>
    /// 贝塞尔曲线
    /// </summary>
    public class ShapeBSplineCurve : ShapeBase, IshapeType
    {
        public string Title = "贝塞尔曲线";
        

        public Geom_BSplineCurve? Geom { get; private set; }
     

        public ShapeBSplineCurve(TopoDS_Shape tshape, Geom_BSplineCurve curve, Pnt startPoint, Pnt endPoint) : base(tshape, curve, startPoint, endPoint)
        {
            Geom = curve;
            Type = GeomType.BezierCurve;
        }

        public override Pnt GetLocation()
        {
            return Geom.StartPoint();
        }

        public override bool IsEqual(ShapeBase shape)
        {
            try
            {
                if (this.Type != shape.Type) return false;
                if (this.StartPoint != shape.StartPoint || this.EndPoint != shape.EndPoint) return false;
                if (Geom.Equals(shape.Geom)) return false;

                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }
    }


}
